var search_stock_name = '';//全局变量，被搜索的股票名称
var search_ts_code = '';//全局变量，被搜索的股票ts代码
var search_stock_name_list = '';//全局变量，被搜索的股票名称
var search_ts_code_list = '';//全局变量，被搜索的股票ts代码

var sf = new Snowflakes({
    color: "#ffffff",
    count: 75,
    minOpacity: 0.2,
    maxOpacity: 0.6
});

//窗体加载后函数，简写方式
function fromAnalysis() {
    /*
     *判断是否从其他页面跳转而来，如果是则获取url中的信息
     */
    var url = decodeURI(window.location.href);//获取当前浏览器的url,decodeURI函数防止中文乱码
    isanalysis = url.indexOf("analysis");//analysis，如果有，说明是从analysis页面跳转而来的，就执行下面的程序
    if (isanalysis != -1) {//由analysis页面跳转而来

        var a = url.indexOf("ts_code");
        search_ts_code = url.substring(a + "ts_code".length + 1, a + "ts_code".length + 10);//获取股票代码
        var a = url.indexOf("name");
        search_stock_name = url.substring(a + "name".length + 1);//获取股票名称
        $("header").hide();
        $("nav").hide();
        query_stockinfo(search_stock_name, search_ts_code);
        query_stock_detail();


    }


}

window.onload = fromAnalysis();//窗体加载后执行


/************
 *更新数据函数
 *************/
function update_daily() {
    $.ajax({
        url: "./",
        type: "GET",
        data: {cmd_class: "update", command: "update_daily"},//cmd_class表示返回命令类别，command表示具体返回的命令，提高判断语句效率
        success: function (callback) {
            alert(callback);
        },
        error: function () {
            alert('提交失败');
        }
    })
}

function update_daily_basic() {
    $.ajax({
        url: "./",
        type: "GET",
        data: {cmd_class: "update", command: "update_daily_basic"},
        success: function (callback) {
            alert(callback);
        },
        error: function () {
            alert('提交失败');
        }
    })
}

function update_block_trade() {
    $.ajax({
        url: "./",
        type: "GET",
        data: {cmd_class: "update", command: "update_block_trade"},
        success: function (callback) {
            alert(callback);
        },
        error: function () {
            alert('提交失败');
        }
    })
}

function update_moneyflow() {
    $.ajax({
        url: "./",
        type: "GET",
        data: {cmd_class: "update", command: "update_moneyflow"},
        success: function (callback) {
            alert(callback);
        },
        error: function () {
            alert('提交失败');
        }
    })

}

function update_stockbasic() {
    $.ajax({
        url: "./",
        type: "GET",
        data: {cmd_class: "update", command: "update_stockbasic"},
        success: function (callback) {
            alert(callback);
        },
        error: function () {
            alert('提交失败');
        }
    })
}

function update_company() {
    $.ajax({
        url: "./",
        type: "GET",
        data: {cmd_class: "update", command: "update_company"},
        success: function (callback) {
            alert(callback);
        },
        error: function () {
            alert('提交失败');
        }
    })
}

function update_all_basic() {
    $.ajax({
        url: "./",
        type: "GET",
        data: {cmd_class: "update", command: "update_all_basic"},
        success: function (callback) {
            alert(callback);
        },
        error: function () {
            alert('提交失败');
        }
    })

}

function initial_analysis() {
    $.ajax({
        url: "./",
        type: "GET",
        data: {cmd_class: "update", command: "initial_analysis"},
        success: function (callback) {
            alert(callback);
        },
        error: function () {
            alert('提交失败');
        }
    })

}

/*************************
 按输入搜索
 ***************************/
function delaySearch(search_txt) {
    var keycode = event.keyCode;//如果是上下按钮，则什么都不做
    if ((keycode == 38) || (keycode == 40)) {
        return;
    }

    var searchtxt = $("#input_search_stockinfo").val().trim();
    //当输入的是数字，同时长度大于3的时候
    if (!isNaN(searchtxt) && (searchtxt.length > 3)) {
        search_input();
    }
    if (isNaN(searchtxt) && (searchtxt.length >= 1)) {
        search_input();
    }

    // t = setTimeout("search_input()", 1500);
}

function search_input() {
    var searchtxt = $("#input_search_stockinfo").val().trim();
    if (searchtxt == '') {
        return;
    }

    $.ajax({
        url: "./",
        type: "GET",
        data: {cmd_class: "search", command: "search_stockname", search_txt: searchtxt},
        success: function (callback) {
            add_option_from_search_return(callback);
            search_stock_name_list = callback[0];
            search_ts_code_list = callback[1];
            //alert(search_stock_name_list);


        },
        error: function () {
            alert('提交失败');
        }
    })
}

function add_option_from_search_return(result) {
    $("#searchlist").empty();
    for (i = 0; i < result[0].length; ++i) {
        var option = "<option  label=\'" + result[1][i] + "\'  value=\'" + result[0][i] + "\' >" + "</option>";//datalist中label时最终显示信息，value是用来查找的，chromn可以同时按两个属性查找
        // var option = "<option value=\'"+result[0][i]+"\'  />";

        $("#searchlist").append(option);
    }
}

/***********************
 查询股票信息并显示在广告栏中
 **********************/
function query_stockinfo(searchtxt = $("#input_search_stockinfo").val().trim(), tscode = '') {

    if (search_stock_name_list.length > 0) {
        indexof = search_stock_name_list.indexOf(searchtxt);
        search_stock_name = searchtxt;
    }

    if (search_ts_code_list.length > 0) {
        search_ts_code = search_ts_code_list[indexof];
    } else {
        search_ts_code = tscode
    }

    if (tscode.length > 0) {
        query_daily(tscode);
        query_daily_basic(tscode);
        query_block_trade(tscode);
        query_moneyflow(tscode);
        query_stock_detail();
    } else {
        query_daily(searchtxt);
        query_daily_basic(searchtxt);
        query_block_trade(searchtxt);
        query_moneyflow(searchtxt);
        query_stock_detail();
    }

    window.setInterval(query_stock_detail, 600000);

}

function query_daily(searchtxt) {
    $.ajax({
        url: "/stock/query/",
        type: "GET",
        data: {cmd_class: "query", command: "daily", stockname: searchtxt},
        success: function (callback) {
            var data = eval(callback);//解析json数据，json需要用eval函数解析后才方便使用
            var daily_column = eval(data['columns']);
            var daily_data = eval(data['data']);
            // $("#panel_daily").empty();
            $("#div_daily").empty();
            $("#div_daily").append("<div style='color: #ffffff' align='center'>" + search_stock_name + ":每日交易信息</div>");

            // $("#panel_daily").append("<table id='table_daily'></table> ");
            $("#div_daily").append("<table id='table_daily'></table> ");
            $("#table_daily").append("<tbody id='tbody_daily'></tbody> ");
            $("#tbody_daily").append("<tr id='tr_daily_title'></tr> ");
            for (i = 1; i < daily_column.length; ++i) {
                $("#tr_daily_title").append("<th scope='col'>" + daily_column[i].fields['remark'] + "</th> ");
            }

            for (i = 0; i < daily_data.length; ++i) {
                $("#tbody_daily").append("<tr></tr> ");
                // $("#tbody_daily:last-child").append("<td>" + daily_data[i].fields['ts_code'] + "</td> ");
                $("#tbody_daily:last-child").append("<td>" + daily_data[i].fields['trade_date'] + "</td> ");
                $("#tbody_daily:last-child").append("<td>" + daily_data[i].fields['open'] + "</td> ");
                $("#tbody_daily:last-child").append("<td>" + daily_data[i].fields['high'] + "</td> ");
                $("#tbody_daily:last-child").append("<td>" + daily_data[i].fields['low'] + "</td> ");
                $("#tbody_daily:last-child").append("<td>" + daily_data[i].fields['close'] + "</td> ");
                $("#tbody_daily:last-child").append("<td>" + daily_data[i].fields['pre_close'] + "</td> ");
                // $("#tbody_daily:last-child").append("<td>" + daily_data[i].fields['change'] + "</td> ");
                if (daily_data[i].fields['change'] >= 0) {
                    $("#tbody_daily:last-child").append("<td style='color: #dc3545'>" + daily_data[i].fields['change'] + "</td> ");
                } else {
                    $("#tbody_daily:last-child").append("<td style='color:greenyellow;'>" + daily_data[i].fields['change'] + "</td> ");
                }
                // $("#tbody_daily:last-child").append("<td>" + daily_data[i].fields['pct_chg'].toFixed(2) + "</td> ");
                if (daily_data[i].fields['pct_chg'].toFixed(2) >= 0) {
                    $("#tbody_daily:last-child").append("<td style='color: #dc3545'>" + daily_data[i].fields['pct_chg'].toFixed(2) + "</td> ");
                } else {
                    $("#tbody_daily:last-child").append("<td style='color:greenyellow;'>" + daily_data[i].fields['pct_chg'].toFixed(2) + "</td> ");
                }
                $("#tbody_daily:last-child").append("<td>" + daily_data[i].fields['vol'] + "</td> ");
                $("#tbody_daily:last-child").append("<td>" + daily_data[i].fields['amount'] + "</td> ");
            }
            //设置全局变量名,是否存在，可以后面判断，如果是从分析链接过来的，则直接在分析列表中设置
            search_stock_name = searchtxt;
            search_ts_code = daily_data[0].fields['ts_code'];
        },
        error: function () {
            alert('每日交易查询失败');
        }
    })
}

function query_daily_basic(searchtxt) {

    $.ajax({
        url: "/stock/query/",
        type: "GET",
        data: {cmd_class: "query", command: "daily_basic", stockname: searchtxt},
        success: function (callback) {
            var data = eval(callback);
            var daily_column = eval(data['columns']);
            var daily_data = eval(data['data']);
            // alert(daily_column);
            // alert(daily_data);

            // $("#panel_daily_basic").empty();
            // $("#panel_daily_basic").append("<table id='table_daily_baisc'></table> ");
            $("#div_daily_basic").empty();
            $("#div_daily_basic").append("<div style='color: #ffffff' align='center'>" + search_stock_name + ":每日交易指标</div>");
            $("#div_daily_basic").append("<table id='table_daily_baisc'></table> ");
            $("#table_daily_baisc").append("<tbody id='tbody_daily_baisc'></tbody> ");
            $("#tbody_daily_baisc").append("<tr id='tr_daily_baisc_title'></tr> ");
            for (i = 1; i < daily_column.length; ++i) {
                $("#tr_daily_baisc_title").append("<th scope='col'>" + daily_column[i].fields['remark'] + "</th> ");
            }

            for (i = 0; i < daily_data.length; ++i) {
                $("#tbody_daily_baisc").append("<tr></tr> ");
                // $("#tbody_daily_baisc:last-child").append("<td>" + daily_data[i].fields['ts_code'] + "</td> ");
                $("#tbody_daily_baisc:last-child").append("<td>" + daily_data[i].fields['trade_date'] + "</td> ");
                $("#tbody_daily_baisc:last-child").append("<td>" + daily_data[i].fields['close'] + "</td> ");
                $("#tbody_daily_baisc:last-child").append("<td>" + daily_data[i].fields['turnover_rate'] + "</td> ");
                if (daily_data[i].fields['turnover_rate_f'] > 2) {
                    $("#tbody_daily_baisc:last-child").append("<td style='color: #dc3545;'>" + daily_data[i].fields['turnover_rate_f'] + "</td> ");
                } else {
                    $("#tbody_daily_baisc:last-child").append("<td>" + daily_data[i].fields['turnover_rate_f'] + "</td> ");

                }
                // $("#tbody_daily_baisc:last-child").append("<td>" + daily_data[i].fields['turnover_rate_f'] + "</td> ");
                $("#tbody_daily_baisc:last-child").append("<td>" + daily_data[i].fields['volume_ratio'] + "</td> ");
                $("#tbody_daily_baisc:last-child").append("<td>" + daily_data[i].fields['pe'] + "</td> ");
                $("#tbody_daily_baisc:last-child").append("<td>" + daily_data[i].fields['pe_ttm'] + "</td> ");
                $("#tbody_daily_baisc:last-child").append("<td>" + daily_data[i].fields['pb'] + "</td> ");
                $("#tbody_daily_baisc:last-child").append("<td>" + daily_data[i].fields['ps'] + "</td> ");
                $("#tbody_daily_baisc:last-child").append("<td>" + daily_data[i].fields['ps_ttm'] + "</td> ");
                $("#tbody_daily_baisc:last-child").append("<td>" + daily_data[i].fields['total_share'] + "</td> ");
                $("#tbody_daily_baisc:last-child").append("<td>" + daily_data[i].fields['float_share'] + "</td> ");
                $("#tbody_daily_baisc:last-child").append("<td>" + daily_data[i].fields['free_share'] + "</td> ");
                $("#tbody_daily_baisc:last-child").append("<td>" + daily_data[i].fields['total_mv'] + "</td> ");
                $("#tbody_daily_baisc:last-child").append("<td>" + daily_data[i].fields['circ_mv'] + "</td> ");
                $("#tbody_daily_baisc:last-child").append("<td>" + daily_data[i].fields['dv_ratio'] + "</td> ");
                $("#tbody_daily_baisc:last-child").append("<td>" + daily_data[i].fields['dv_ttm'] + "</td> ");
            }
        },
        error: function () {
            alert('获取交易指标失败');
        }
    })
}

function query_block_trade(searchtxt) {

    $.ajax({
        url: "/stock/query",
        type: "GET",
        data: {cmd_class: "query", command: "block_trade", stockname: searchtxt},
        success: function (callback) {
            var data = eval(callback);
            var daily_column = eval(data['columns']);
            var daily_data = eval(data['data']);
            // alert(daily_column);
            // alert(daily_data);

            // $("#panel_block_trade").empty();
            // $("#panel_block_trade").append("<table id='table_block_trade'></table> ");

            $("#div_block_trade").empty();
            $("#div_block_trade").append("<div style='color: #ffffff' align='center'>" + search_stock_name + ":大宗交易</div>");
            $("#div_block_trade").append("<table id='table_block_trade'></table> ");
            $("#table_block_trade").append("<tbody id='tbody_block_trade'></tbody> ");
            $("#tbody_block_trade").append("<tr id='tr_block_trade_title'></tr> ");
            for (i = 1; i < daily_column.length; ++i) {
                $("#tr_block_trade_title").append("<th scope='col'>" + daily_column[i].fields['remark'] + "</th> ");
            }

            for (i = 0; i < daily_data.length; ++i) {
                $("#tbody_block_trade").append("<tr></tr> ");
                // $("#tbody_block_trade:last-child").append("<td>" + daily_data[i].fields['ts_code'] + "</td> ");
                $("#tbody_block_trade:last-child").append("<td>" + daily_data[i].fields['trade_date'] + "</td> ");
                $("#tbody_block_trade:last-child").append("<td>" + daily_data[i].fields['price'] + "</td> ");
                $("#tbody_block_trade:last-child").append("<td>" + daily_data[i].fields['vol'] + "</td> ");
                $("#tbody_block_trade:last-child").append("<td>" + daily_data[i].fields['amount'] + "</td> ");
                $("#tbody_block_trade:last-child").append("<td>" + daily_data[i].fields['buyer'] + "</td> ");
                $("#tbody_block_trade:last-child").append("<td>" + daily_data[i].fields['seller'] + "</td> ");

            }
        },

        error: function () {
            alert('大宗交易查询失败');
        }
    })
}

function query_moneyflow(searchtxt) {

    $.ajax({
        url: "/stock/query",
        type: "GET",
        data: {cmd_class: "query", command: "moneyflow", stockname: searchtxt},
        success: function (callback) {
            var data = eval(callback);
            var daily_column = eval(data['columns']);
            var daily_data = eval(data['data']);
            // $("#panel_moneyflow").empty();
            // $("#panel_moneyflow").append("<table id='table_moneyflow'></table> ");

            $("#div_moneyflow").empty();
            $("#div_moneyflow").append("<div style='color: #ffffff' align='center'>" + search_stock_name + ":资金流向</div>");
            $("#div_moneyflow").append("<table id='table_moneyflow'></table> ");
            $("#table_moneyflow").append("<tbody id='tbody_moneyflow'></tbody> ");
            $("#tbody_moneyflow").append("<tr id='tr_moneyflow_title'></tr> ");
            /*
            小单、中单的都不显示
             */
            $("#tr_moneyflow_title").append("<th scope='col'>" + daily_column[1].fields['remark'] + "</th> ");
            for (i = 10; i < daily_column.length; ++i) {
                $("#tr_moneyflow_title").append("<th scope='col'>" + daily_column[i].fields['remark'] + "</th> ");
            }

            for (i = 0; i < daily_data.length; ++i) {
                $("#tbody_moneyflow").append("<tr></tr> ");
                // $("#tbody_moneyflow:last-child").append("<td>" + daily_data[i].fields['ts_code'] + "</td> ");
                $("#tbody_moneyflow:last-child").append("<td>" + daily_data[i].fields['trade_date'] + "</td> ");
                // $("#tbody_moneyflow:last-child").append("<td>" + daily_data[i].fields['buy_sm_vol'] + "</td> ");
                // $("#tbody_moneyflow:last-child").append("<td>" + daily_data[i].fields['buy_sm_amount'] + "</td> ");
                // $("#tbody_moneyflow:last-child").append("<td>" + daily_data[i].fields['sell_sm_vol'] + "</td> ");
                // $("#tbody_moneyflow:last-child").append("<td>" + daily_data[i].fields['sell_sm_amount'] + "</td> ");
                // $("#tbody_moneyflow:last-child").append("<td>" + daily_data[i].fields['buy_md_vol'] + "</td> ");
                // $("#tbody_moneyflow:last-child").append("<td>" + daily_data[i].fields['buy_md_amount'] + "</td> ");
                // $("#tbody_moneyflow:last-child").append("<td>" + daily_data[i].fields['sell_md_vol'] + "</td> ");
                // $("#tbody_moneyflow:last-child").append("<td>" + daily_data[i].fields['sell_md_amount'] + "</td> ");
                /*
                判断主力流入流出，
                 */
                buy_lg = daily_data[i].fields['buy_lg_amount'];
                buy_elg = daily_data[i].fields['buy_elg_amount'];
                sel_lg = daily_data[i].fields['sell_lg_amount'];
                sel_elg = daily_data[i].fields['sell_elg_amount'];
                main_flow = buy_elg + buy_lg - sel_elg - sel_lg;
                if (main_flow > 0) {
                    $("#tbody_moneyflow:last-child").append("<td style='color: #dc3545'>" + daily_data[i].fields['buy_lg_vol'] + "</td> ");
                    $("#tbody_moneyflow:last-child").append("<td style='color: #dc3545'>" + daily_data[i].fields['buy_lg_amount'] + "</td> ");
                    $("#tbody_moneyflow:last-child").append("<td style='color: #dc3545'>" + daily_data[i].fields['sell_lg_vol'] + "</td> ");
                    $("#tbody_moneyflow:last-child").append("<td style='color: #dc3545'>" + daily_data[i].fields['sell_lg_amount'] + "</td> ");
                    $("#tbody_moneyflow:last-child").append("<td style='color: #dc3545'>" + daily_data[i].fields['buy_elg_vol'] + "</td> ");
                    $("#tbody_moneyflow:last-child").append("<td style='color: #dc3545'>" + daily_data[i].fields['buy_elg_amount'] + "</td> ");
                    $("#tbody_moneyflow:last-child").append("<td style='color: #dc3545'>" + daily_data[i].fields['sell_elg_vol'] + "</td> ");
                    $("#tbody_moneyflow:last-child").append("<td style='color: #dc3545'>" + daily_data[i].fields['sell_elg_amount'] + "</td> ");
                } else {
                    $("#tbody_moneyflow:last-child").append("<td style='color: greenyellow'>" + daily_data[i].fields['buy_lg_vol'] + "</td> ");
                    $("#tbody_moneyflow:last-child").append("<td style='color: greenyellow'>" + daily_data[i].fields['buy_lg_amount'] + "</td> ");
                    $("#tbody_moneyflow:last-child").append("<td style='color: greenyellow'>" + daily_data[i].fields['sell_lg_vol'] + "</td> ");
                    $("#tbody_moneyflow:last-child").append("<td style='color: greenyellow'>" + daily_data[i].fields['sell_lg_amount'] + "</td> ");
                    $("#tbody_moneyflow:last-child").append("<td style='color: greenyellow'>" + daily_data[i].fields['buy_elg_vol'] + "</td> ");
                    $("#tbody_moneyflow:last-child").append("<td style='color: greenyellow'>" + daily_data[i].fields['buy_elg_amount'] + "</td> ");
                    $("#tbody_moneyflow:last-child").append("<td style='color: greenyellow'>" + daily_data[i].fields['sell_elg_vol'] + "</td> ");
                    $("#tbody_moneyflow:last-child").append("<td style='color: greenyellow'>" + daily_data[i].fields['sell_elg_amount'] + "</td> ");
                }
                ////////////
                $("#tbody_moneyflow:last-child").append("<td>" + daily_data[i].fields['net_mf_vol'] + "</td> ");
                // $("#tbody_moneyflow:last-child").append("<td>" + daily_data[i].fields['net_mf_amount'] + "</td> ");
                if (daily_data[i].fields['net_mf_amount'] > 0) {
                    $("#tbody_moneyflow:last-child").append("<td style='color: #dc3545'>" + daily_data[i].fields['net_mf_amount'] + "</td> ");
                } else {
                    $("#tbody_moneyflow:last-child").append("<td style='color: greenyellow'>" + daily_data[i].fields['net_mf_amount'] + "</td> ");
                }

            }
        },
        error: function () {
            alert('资金流查询失败');
        }
    })
}

function query_stock_detail() {
    /*
      查询股票详细信息
     */

    $.ajax({
        url: '/stock/stock_detail/',
        type: "GET",
        data: {stock_name: search_stock_name, ts_code: search_ts_code},
        success: function (data) {
            $("#div_detail_info").empty();
            $("#div_detail_info").html(data);
        }
    });
}